home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / moni / Scout-src.lha / source / Scout.c < prev    next >
C/C++ Source or Header  |  2002-09-16  |  15KB  |  481 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28. #include "system_headers.h"
  29.  
  30. /*********************************************************************
  31.                            Definitionen
  32. *********************************************************************/
  33. #define MYLIBVERSION    37
  34. #define MUIVERSION      8
  35.  
  36. /*********************************************************************
  37.                          Globale Variablen
  38. *********************************************************************/
  39. extern struct  ExecBase *SysBase;
  40.  
  41. struct Library          *MUIMasterBase;
  42. struct Library          *IdentifyBase;
  43. struct MMUBase          *MMUBase;
  44. struct Library          *CxBase;
  45. struct Library          *LayersBase;
  46. struct LocaleBase       *LocaleBase;
  47.  
  48. struct Device           *TimerBase;
  49.  
  50. struct Task             *myprocess;
  51. struct MsgPort          *myarexxport,*ScoutPort;
  52. BOOL   ScoutPortAdded = FALSE;
  53.  
  54. struct IOStdReq         *InputIORequest;
  55. BOOL   input = TRUE, timer2 = TRUE;
  56. struct timerequest      *TimerIORequest;
  57. BYTE   TimerSignal;
  58.  
  59. BOOL   input;
  60. char   *portname;
  61.  
  62. APTR   globalPool;
  63.  
  64. STRPTR decimalSeparator;
  65.  
  66. /*
  67. **  MUI
  68. */
  69.  
  70. APTR AP_Scout;
  71.  
  72. APTR WI_Main;
  73.  
  74. BPTR     printfile, prevdir;
  75.  
  76. UBYTE *AllocationsText   = "Allocations";      /*-*/
  77. UBYTE *AssignsText       = "Assigns";
  78. UBYTE *ClassesText       = "BoopsiClasses";    /*-*/
  79. UBYTE *CommoditiesText   = "Commodities";      /*-*/
  80. UBYTE *DevicesText       = "Devices";
  81. UBYTE *CommandsText      = "Resident Cmds";
  82. UBYTE *ExpansionsText    = "Expansions";
  83. UBYTE *FontsText         = "Fonts";
  84. UBYTE *InputHandlersText = "InputHandlers";
  85. UBYTE *InterruptsText    = "Interrupts";
  86. UBYTE *LibrariesText     = "Libraries";
  87. UBYTE *LocksText         = "Locks";
  88. UBYTE *LowMemoryText     = "LowMemory";        /*-*/
  89. UBYTE *MemoryText        = "Memory";
  90. UBYTE *MountText         = "Mounted Devs";
  91. UBYTE *PortsText         = "Ports";
  92. UBYTE *ResidentsText     = "Residents";
  93. UBYTE *ResourcesText     = "Resources";
  94. UBYTE *ScreenModeText    = "ScreenMode";
  95. UBYTE *SemaphoresText    = "Semaphores";
  96. UBYTE *SystemText        = "System";           /*-*/
  97. UBYTE *TasksText         = "Tasks";
  98. UBYTE *TimerText         = "Timer";            /*-*/
  99. UBYTE *VectorsText       = "Vectors";
  100. UBYTE *WindowsText       = "Windows";
  101. UBYTE *PatchesText       = "Patches";
  102. UBYTE *CatalogsText      = "Catalogs";
  103. UBYTE *AudioModesText    = "AudioModes";
  104. UBYTE *ResetHandlersText = "ResetHandlers";
  105.  
  106. /*********************************************************************
  107.                                  Fail
  108.    Diese Routine wird zum Schluß des Programmes aufgerufen!
  109. *********************************************************************/
  110. static void fail1( void )
  111. {
  112.     Forbid();
  113.     if (FindPort("AMITCP")) {
  114.        Permit();
  115.        failtcp();
  116.     } else {
  117.        Permit();
  118.     }
  119.  
  120.     if (globalPool) tbDeletePool(globalPool);
  121.  
  122.     if (TimerIORequest) {
  123.         if (!timer2) {
  124.             if (timer_ticking) {
  125.                 if (!CheckIO((struct IORequest *)TimerIORequest)) AbortIO((struct IORequest *)TimerIORequest);
  126.                 WaitIO((struct IORequest *)TimerIORequest);
  127.             }
  128.             CloseDevice((struct IORequest *)TimerIORequest);
  129.         }
  130.         DeleteIORequest(TimerIORequest);
  131.     }
  132.  
  133.     if (InputIORequest) {
  134.         if (!input) CloseDevice((struct IORequest *)InputIORequest);
  135.         DeleteIORequest(InputIORequest);
  136.     }
  137.  
  138.     if (ScoutPort) {
  139.         if (ScoutPortAdded) RemPort(ScoutPort);
  140.         DeleteMsgPort(ScoutPort);
  141.     }
  142.  
  143.     if (prevdir) CurrentDir(prevdir);
  144.  
  145.     CloseScoutCatalog();
  146.  
  147.     if (RexxSysBase) CloseLibrary((struct Library *)RexxSysBase);
  148.     if (ExpansionBase) CloseLibrary((struct Library *)ExpansionBase);
  149.     if (MMUBase) CloseLibrary((struct Library *)MMUBase);
  150.     if (IdentifyBase) CloseLibrary(IdentifyBase);
  151.     if (CxBase) CloseLibrary(CxBase);
  152.     if (AslBase) CloseLibrary(AslBase);
  153.     if (DiskfontBase) CloseLibrary(DiskfontBase);
  154.     if (UtilityBase) CloseLibrary(UtilityBase);
  155.     if (IconBase) CloseLibrary(IconBase);
  156.     if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  157.     if (GfxBase) CloseLibrary((struct Library *) GfxBase);
  158.     if (LayersBase) CloseLibrary(LayersBase);
  159.     if (LocaleBase) CloseLibrary((struct Library *)LocaleBase);
  160. }
  161.  
  162. static void fail( void )
  163. {
  164.     if (AP_Scout) {
  165.         struct DiskObject *dob = NULL;
  166.  
  167.         get(AP_Scout, MUIA_Application_DiskObject, &dob);
  168.         if (dob) FreeDiskObject(dob);
  169.  
  170.         MUI_DisposeObject(AP_Scout);
  171.         AP_Scout = NULL;
  172.     }
  173.  
  174.     if (MUIMasterBase) {
  175.         DeleteCustomClasses();
  176.         CloseLibrary(MUIMasterBase);
  177.     }
  178.  
  179.     fail1();
  180. }
  181.  
  182. /*********************************************************************
  183.                                  Init
  184.    Diese Routine wird am Beginn des Programmes aufgerufen!
  185. *********************************************************************/
  186. static BOOL init1( void )
  187. {
  188.     GfxBase = (struct GfxBase *)OpenLibrary(GRAPHICSNAME, MYLIBVERSION);
  189.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", MYLIBVERSION);
  190.     IconBase = OpenLibrary(ICONNAME, MYLIBVERSION);
  191.     UtilityBase = OpenLibrary(UTILITYNAME, MYLIBVERSION);
  192.  
  193.     if ((!GfxBase) || (!IntuitionBase) || (!IconBase) || (!UtilityBase)) return FALSE;
  194.  
  195.     if (!(AslBase = MyOpenLibrary("asl.library", MYLIBVERSION))) return FALSE;
  196.     if (!(DiskfontBase = MyOpenLibrary("diskfont.library", MYLIBVERSION))) return FALSE;
  197.     if (!(CxBase = MyOpenLibrary("commodities.library", MYLIBVERSION))) return FALSE;
  198.     if (!(ExpansionBase = (struct ExpansionBase *)MyOpenLibrary(EXPANSIONNAME, MYLIBVERSION))) return FALSE;
  199.     if (!(RexxSysBase = (struct RxsLib *)MyOpenLibrary(RXSNAME, 0))) return FALSE;
  200.     if (!(LayersBase = MyOpenLibrary("layers.library", MYLIBVERSION))) return FALSE;
  201.  
  202.     IdentifyBase = OpenLibrary("identify.library", IDENTIFYVERSION);
  203.     if (!IdentifyBase) IdentifyBase = OpenLibrary("PROGDIR:identify.library", IDENTIFYVERSION);
  204.     if (!IdentifyBase) IdentifyBase = OpenLibrary("PROGDIR:Libs/identify.library", IDENTIFYVERSION);
  205.  
  206.     MMUBase = (struct MMUBase *)OpenLibrary(MMU_NAME, 43);
  207.  
  208.     decimalSeparator = ","; // english separator for 1000s
  209.     if (LocaleBase = (struct LocaleBase *)OpenLibrary("locale.library", 37)) {
  210.         struct Locale *loc;
  211.  
  212.         if (loc = OpenLocale(NULL)) {
  213.             decimalSeparator = loc->loc_GroupSeparator;
  214.         }
  215.     }
  216.  
  217.     OpenScoutCatalog();
  218.  
  219.     myprocess = FindTask(NULL);
  220.     prevdir = CurrentDir(((struct Process *)myprocess)->pr_HomeDir);
  221.  
  222.     if (ScoutPort = CreateMsgPort()) {
  223.         ScoutPort->mp_Node.ln_Name = "scout.reply";
  224.         ScoutPort->mp_Node.ln_Pri = 1;
  225.  
  226.         TimerSignal = ScoutPort->mp_SigBit;
  227.  
  228.         AddPort(ScoutPort);
  229.         ScoutPortAdded = TRUE;
  230.  
  231.         if (InputIORequest = CreateIORequest(ScoutPort, sizeof(struct IOStdReq))) {
  232.             if (input = OpenDevice("input.device", 0, (struct IORequest *)InputIORequest, 0)) {
  233.                 aprintf(msgCantOpenInputDevice);
  234.                 return FALSE;
  235.             }
  236.         } else {
  237.             return FALSE;
  238.         }
  239.  
  240.         if (TimerIORequest = CreateIORequest(ScoutPort, sizeof(struct timerequest))) {
  241.             if (timer2 = OpenDevice(TIMERNAME, 0, (struct IORequest *)TimerIORequest, 0)) {
  242.                 aprintf(msgCantOpenTimerDevice);
  243.                 return FALSE;
  244.             }
  245.         } else {
  246.             return FALSE;
  247.         }
  248.  
  249.         if (!(globalPool = tbCreatePool(MEMF_CLEAR | MEMF_PUBLIC, 4096, 4096))) {
  250.             return FALSE;
  251.         }
  252.     } else {
  253.         return FALSE;
  254.     }
  255.  
  256.     return TRUE;
  257. }
  258.  
  259. static BOOL init2( void )
  260. {
  261.     if (!(MUIMasterBase = MyOpenLibrary(MUIMASTER_NAME, MUIVERSION))) return FALSE;
  262.     if (!CreateCustomClasses()) return FALSE;
  263.  
  264.     return (TRUE);
  265. }
  266.  
  267. /*********************************************************************
  268.  BEGIN:                        MAIN
  269. *********************************************************************/
  270.  
  271. ULONG scout_main( void )
  272. {
  273.     BOOL RETURN_FLAG = FALSE;
  274.     ULONG RETURN_CODE = RETURN_OK;
  275.     struct Library *miamilib = NULL;
  276.  
  277.     if (!init1()) {
  278.         fail1();
  279.         return RETURN_FAIL;
  280.     }
  281.  
  282.     if ((opts.User || opts.Password) && (!opts.Host)) {
  283.         aprintf(msgNoHostNameSpecified);
  284.         RETURN_CODE = RETURN_FAIL;
  285.         RETURN_FLAG = TRUE;
  286.     } else if (opts.Host) {
  287.         Forbid();
  288.         if (FindPort("AMITCP") != NULL || (miamilib = OpenLibrary("miami.library",0L)) != NULL) {
  289.             Permit();
  290.  
  291.             if (miamilib) CloseLibrary(miamilib);
  292.             if (opts.Command) {
  293.                 RETURN_CODE = netshellclient();
  294.                 RETURN_FLAG = TRUE;
  295.             } else if (!(clientstate = ConnectToServer())) {
  296.                 RETURN_CODE = RETURN_FAIL;
  297.                 RETURN_FLAG = TRUE;
  298.             }
  299.         } else {
  300.             Permit();
  301.  
  302.             aprintf(msgStartTCPIPStack);
  303.             RETURN_CODE = RETURN_FAIL;
  304.             RETURN_FLAG = TRUE;
  305.         }
  306.     } else if (opts.Command) {
  307.         shellstate = TRUE;
  308.         RETURN_CODE = ExecuteCommand(opts.Command);
  309.         RETURN_FLAG = TRUE;
  310.     } else {
  311.         Forbid();
  312.         if (FindPort("AMITCP") && isNetCall()) {
  313.             Permit();
  314.             RETURN_CODE = netdaemon();
  315.             RETURN_FLAG = TRUE;
  316.         } else {
  317.             Permit();
  318.         }
  319.     }
  320.  
  321.     if (RETURN_FLAG) {
  322.         fail1();
  323.         return RETURN_CODE;
  324.     }
  325.  
  326.     if (!init2()) {
  327.         fail();
  328.         return RETURN_FAIL;
  329.     }
  330.  
  331.     if ((opts.IntervalTime) && (IsReal(opts.IntervalTime))) {
  332.         if (strlen(opts.IntervalTime) <= 6) {
  333.            strcpy(updatetimetext, opts.IntervalTime);
  334.         }
  335.     }
  336.  
  337.     if (!GetApplication()) {
  338.         aprintf(msgCantCreateApplication);
  339.         fail();
  340.         return RETURN_FAIL;
  341.     }
  342.  
  343.     Forbid();
  344.     if (portname = FindMyARexxPort("SCOUT")) {
  345.         myarexxport = FindPort(portname);
  346.     } else {
  347.         portname = " < ERROR > ";
  348.         myarexxport = NULL;
  349.     }
  350.  
  351.     if ((opts.PortName) && (myarexxport)) {
  352.         portname = opts.PortName;
  353.         myarexxport->mp_Node.ln_Name = opts.PortName;
  354.     }
  355.     Permit();
  356.  
  357.     if (opts.Iconified) set(AP_Scout, MUIA_Application_Iconified, TRUE);
  358.  
  359. /*
  360. ** Everything's ready, lets launch the application. We will
  361. ** open the master window now at this old position.
  362. */
  363.  
  364.     DoMethod(AP_Scout, MUIM_Application_Load, MUIV_Application_Load_ENV);
  365.  
  366.     set(WI_Main, MUIA_Window_Open, TRUE);
  367.  
  368. /*
  369. ** Now the ARexx startup script will be started. Herewith it's
  370. ** possible to open window at the beginning.
  371. */
  372.  
  373.     if (opts.Startup) {
  374.         BPTR startuplock;
  375.  
  376.         if (startuplock = Lock(opts.Startup, SHARED_LOCK)) {
  377.             UnLock (startuplock);
  378.  
  379.             if (!SendStartupMsg("REXX", opts.Startup, TRUE)) {
  380.                 aprintf(msgCantSendARexxScript, opts.Startup, "REXX");
  381.             }
  382.         } else {
  383.             if (!SendStartupMsg(myarexxport->mp_Node.ln_Name, opts.Startup, FALSE)) {
  384.                 aprintf(msgCantSendARexxScript, opts.Startup, myarexxport->mp_Node.ln_Name);
  385.             }
  386.         }
  387.     }
  388.  
  389. /*
  390. ** This is the main loop. As you can see, it does just nothing.
  391. ** Everything is handled by MUI, no work for the programmer.
  392. */
  393.  
  394. #if 0
  395.     {
  396.         ULONG sigs = 0, received = 0;
  397.         ULONG reid;
  398.  
  399.         while (TRUE) {
  400.             reid = DoMethod(AP_Scout, MUIM_Application_NewInput, &sigs);
  401.  
  402.             if (reid == MUIV_Application_ReturnID_Quit) {
  403.                 break;
  404.             }
  405.  
  406. /*
  407.             case ID_LOWMEMORYCAUSE:
  408.                if (entryp = GetActiveEntry (lowmemorylist)) {
  409.                   MyDoCommand ("CauseLowMemory %s", ((struct LowMemoryEntry *) entryp)->lme_Address);
  410.                }
  411.                break;
  412.             case ID_TIMERREMOVE:
  413.                if (entryp = GetActiveEntry (timerlist)) {
  414.                   if (MyRequest ("Yes|No", "Do you really want to remove\n\'%s\'?", ((struct TimerEntry *) entryp)->timer_name)) {
  415.                      if (MyDoCommand ("RemoveTimer %s", ((struct TimerEntry *) entryp)->timer_address)) {
  416.                         SetCountText (timercount, --timercnt);
  417.                         RemoveActiveEntry (timerlist);
  418.                      } else {
  419.                         ShowTimer();
  420.                      }
  421.                   }
  422.                }
  423.                break;
  424. */
  425.  
  426.             if (sigs) {
  427.                 sigs |= SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E | SIGBREAKF_CTRL_F | (1 << TimerSignal);
  428.                 received = Wait(sigs);
  429.             }
  430.  
  431.             if (received & (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E)) {
  432.                 aprintf(msgBreak);
  433.                 break;
  434.             }
  435.  
  436.             if (received & SIGBREAKF_CTRL_F) {
  437.                 set(AP_Scout, MUIA_Application_Iconified, FALSE);
  438.             }
  439.  
  440.             if (received & (1 << TimerSignal)) {
  441.                 GetMsg(ScoutPort);
  442.                 HandleTimerRequest(TRUE);
  443.             }
  444.  
  445.             sigs &= ~(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E | SIGBREAKF_CTRL_F);
  446.             sigs |= (1 << TimerSignal);
  447.         }
  448.     }
  449. #else
  450.     {
  451.         ULONG sigs = 0;
  452.  
  453.         while (DoMethod(AP_Scout, MUIM_Application_NewInput, &sigs) != MUIV_Application_ReturnID_Quit) {
  454.             if (sigs) {
  455.                 sigs = Wait(sigs | SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E | SIGBREAKF_CTRL_F);
  456.  
  457.                 if (sigs & (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E)) {
  458.                     aprintf(msgBreak);
  459.                     break;
  460.                 }
  461.                 if (sigs & SIGBREAKF_CTRL_F) {
  462.                     set(AP_Scout, MUIA_Application_Iconified, FALSE);
  463.                 }
  464.             }
  465.         }
  466.     }
  467. #endif
  468.  
  469.     if (opts.Host) SendDaemon(CMD_END);
  470.  
  471.     // not just close the just, but trigger the CloseSubWindows method, too
  472.     set(WI_Main, MUIA_Window_CloseRequest, TRUE);
  473.  
  474.     fail();
  475.     return RETURN_OK;
  476. }
  477.  
  478. /*********************************************************************
  479.  END:                           MAIN
  480. *********************************************************************/
  481.